package com.xk._01基础篇._12堆;

import com.xk._01基础篇._06二叉搜索树.printer.BinaryTrees;
import com.xk._01基础篇._12堆.heap.BinaryHeap;
import com.xk.tools.Times;

import java.util.Comparator;

/**
 * @description:
 * @author: xu
 * @date: 2022/9/28 23:45
 */
public class Main {
    public static void main(String[] args) {
        Integer[] data = {
                88,44,53,41,16,6,70,18,85,98,81,23,36,43,37,
                3, 54, 63, 24, 88, 52, 60, 74, 5, 17,
                10, 88, 58, 26, 40, 60, 81, 1, 27, 16, 4, 95, 55, 59, 53, 91,
                27, 69, 95, 74, 17, 29, 14, 85, 68, 41, 45, 26, 79, 3, 51, 4, 56, 46, 2, 7
        };

        Times.test("Test04", new Times.Task() {
            @Override
            public void execute() {
                test04(data);
            }
        });
    }

    static void test04(Integer[] data) {
        // 小顶堆
        BinaryHeap<Integer> heap = new BinaryHeap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        // 找出最大的前k个数
        int k = 5;
        for (int i = 0; i < data.length; i++) {
            if (heap.size() < k) { // 前k个数添加到小顶堆
                heap.add(data[i]);
            } else if (data[i] > heap.get()){ // 如果是第k+1个数，并且大于堆顶元素
                heap.replace(data[i]);
            }
        }
        BinaryTrees.println(heap);
        System.out.println("=========================");
    }

    static void test03() {
        // 批量建堆
        Integer[] data = {
                88,44,53,41,16,6,70,18,85,98,81,23,36,43,37
        };

        BinaryHeap<Integer> heap = new BinaryHeap<>(data, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        BinaryTrees.println(heap);
        System.out.println("=========================");
    }

    static void test02() {
        // 批量建堆
        Integer[] data = {
                88,44,53,41,16,6,70,18,85,98,81,23,36,43,37
        };

        BinaryHeap<Integer> heap = new BinaryHeap<>(data);
        BinaryTrees.println(heap);

        System.out.println("=========================");
    }

    static void test01() {
        BinaryHeap<Integer> heap = new BinaryHeap<>();
        heap.add(68);
        heap.add(72);
        heap.add(43);
        heap.add(50);
        heap.add(38);
        heap.add(10);
        heap.add(90);
        heap.add(65);
        BinaryTrees.println(heap);
        System.out.println("======================================");
        System.out.println(heap.replace(70));
        BinaryTrees.println(heap);
    }
}
